package com.zhss.redis.zset;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Tuple;

import java.util.Set;
import java.util.stream.IntStream;

/**
 * @Description TODO
 * @Author winston
 * @DateTime 2021/11/2
 */
public class ZsetDemo {

	HostAndPort hostAndPort = new HostAndPort("121.4.79.149", 6379);
	Jedis jedis = new Jedis(hostAndPort);

	/**
	 * z组添加
	 *
	 * @param id id
	 */
	public void  zSetAdd(long id) {
		jedis.zadd("rank_list", 0, String.valueOf(id));
	}

	/**
	 * z增加分数
	 *
	 * @param id    id
	 * @param score 分数
	 */
	public void zIncrScore(long id, double score) {
		jedis.zincrby("rank_list", score, String.valueOf(id));
	}



	/**
     * @return {@link Set}<{@link Tuple}>
	 */
	public Set<Tuple> getRange() {
		return jedis.zrevrangeWithScores("rank_list", 0, 5);
	}

	public static void main(String[] args) {
		ZsetDemo zsetDemo = new ZsetDemo();
		IntStream.rangeClosed(1, 20).forEach(zsetDemo::zSetAdd);

		zsetDemo.zIncrScore(1, 20);
		zsetDemo.zIncrScore(3, 30);
		zsetDemo.zIncrScore(5, 60);
		zsetDemo.zIncrScore(2, 90);

//		System.out.println(zsetDemo.getRank(2));

		System.out.println(zsetDemo.getRange());

	}
}
